var utils = require('../../utils/util.js');
var base64Js = require('../../utils/base64.js');
var md5Js = require('../../utils/md5.js');
var app = getApp();
var object = {};
object.data = {};
Page({
  data:{
    details: [],
  },
  onLoad:function(options){
     try{
         var _this = this;
        //获取缓存的用户对象信息
        var user_info =  wx.getStorageSync('user_info');
        //如果用户信息不为空则请求首页接口获取产品等信息。
        if(user_info){
          var request_data = user_info.user_itmes;
          var uid         = request_data.id.toString();
          var private_key = user_info.private_key;
          var t           =  new Date().getTime().toString();
          var k           = md5Js.hex_md5(private_key+t);//key
          var parms_url   = app.request_url+'C_OrderList/?';
          parms_url += '&channelId='+request_data.channelId;
          parms_url += '&uid='+request_data.id;
          parms_url += '&userType='+request_data.type;
          parms_url += '&t='+t;
          parms_url += '&k='+k;
          parms_url += '&v='+app.version;;
          parms_url += '&l=999';
          // parms_url = 'https://rest.test.freshqiao.com/C_OrderList/?uid=100000202&channelId=204&userType=1&l=20&o=0&t=1485049786418&k=9075bff51129dfbd3da3a3f8da2abb77&uid=100000202&v=2';
          //调用产品页接口
          wx.request({
            url: parms_url,
            method: 'GET',
            success: function(res){
               var respones = res;
               console.log(respones);
               //请求成功分配模板变量
               if((respones.statusCode=='200')&&(respones.data.Success==1)){
                   //分配模板变量
                  _this.setData({orderlist:res.data.CallInfo})
                 
               }
            }
          })
        }
    }catch(e){
      console.log('error=>'+e.message);return false;
    }
  },
  //取消订单操作
  cancel:function(e){
    //订单ID
    var order_id =  e.currentTarget.dataset.id;
    //获取缓存的用户对象信息
    var user_info = wx.getStorageSync('user_info');
    var request_data = user_info.user_itmes;
    // console.log(request_data)
    //如果用户信息不为空则请求首页接口获取产品等信息。
    if (request_data) {
        var payload_str = 'uid=' + request_data.id + '&actionType=cancel&channelId=' + request_data.channelId + '&v=' + app.version;
        var payload = base64Js.encode(payload_str); //base64Js加密
        //拼装URL
        var parms_url = app.request_url + 'C_Order/' + order_id+'?';
        var t = new Date().getTime().toString();
        var private_key = user_info.private_key;
        var k = md5Js.hex_md5(private_key+order_id + t + payload);
        parms_url += 'uid=' + request_data.id;
        parms_url += '&userType=' + request_data.type;
        parms_url += '&t=' + t;
        parms_url += '&k=' + k;
        parms_url += '&v=' + app.version;
        wx.request({
          url: parms_url,
          method: 'PUT',
          data: { payload: payload },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            var respones = res;
            if ((respones.statusCode == '200') && (respones.data.Success == 1)) {
                  wx.showToast({
                    title: '取消订单成功！',
                  });
                  setTimeout(function () {
                    wx.navigateTo({
                      url: '../orderlist/orderlist',
                    })
                  }, 1000)
            } else {
                wx.showToast({
                  title: '取消订单失败！',
                });
                setTimeout(function () {
                  wx.navigateTo({
                    url: '../orderlist/orderlist',
                  })
                }, 1000)
            }
          },
          fail: function () {
            wx.showModal({
              title: '提示',
              content: '网络异常请求失败!',
              showCancel: false,
              confirmColor: 'black'
            })
          },
        })

       
    }
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  //点击订单时跳转到订单详情页
  orderDetail:function(e){
    var orderid = e.currentTarget.dataset.orderid;
      wx.navigateTo({
        url: '../orderDetail/orderDetail?id='+orderid,
      })
  },


})